<?php
namespace app\first\model;

use think\Model;
/**
 * Class Push
 * @package app\push\validate
 */
class ArticleCallLog extends Model{
    protected $autoWriteTimestamp = false;

    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @param bool $out
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*',$out=false){
        $list = $this
            ->field($filed,$out)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,sex,avatar,is_auth,is_vip,vip_type');
            },'article'=>function($query){
                $query->field('id,title');
            }])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $list;
    }

    /**
     * @param int $aid
     * @param int $uid
     * @param int $type
     * @param string $out
     * @return bool
     */
    public function addNew($aid=0,$uid=0,$type=1,&$out=''){
        $data = [
            'uid'=>$uid,
            'aid'=>$aid,
            'type'=>$type,
            'create_time'=> time()
        ];
        $info = (new Article())->find($aid);
        if(!$info){
            $out = '文章不存在';
            return false;
        }
        $valid = new \app\first\validate\ArticleViewLog();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        $result1 = $this->save($data);
        if($result1){
            return true;
        }else{
            return false;
        }
    }

    public function user(){
        return $this->hasOne('member','id','uid');
    }

    public function article()
    {
        return $this->hasOne('article', 'id', 'aid');
    }
}